package com.ninong.ker.admin.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ninong.ker.admin.domain.FwFormConfig;
import com.ninong.ker.admin.domain.FwGroup;
import com.ninong.ker.admin.mapper.FwFormConfigMapper;
import com.ninong.ker.admin.mapper.FwGroupMapper;
import com.ninong.ker.admin.rest.vo.FwGroupVO;
import com.ninong.ker.admin.service.FwGroupService;
import com.ninong.ker.admin.service.dto.FwGroupDTO;
import com.ninong.ker.common.tools.Constants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;



@Service
public class FwGroupServiceImpl extends ServiceImpl<FwGroupMapper, FwGroup> implements FwGroupService {

	@Autowired
	private FwFormConfigMapper fwFormConfigMapper;

    @Override
	public PageInfo<FwGroup> listPage(FwGroupDTO dto) {
		PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize());
		QueryWrapper<FwGroup> query = new QueryWrapper<>();
		query.eq("deleted", Constants.DEFAULT_YES);
		List<FwGroup> FwGroups = baseMapper.selectList(query);
		return new PageInfo<FwGroup>(FwGroups);
	}

	@Override
	public void delete(String[] ids) {
		List<String> idList = Arrays.asList(ids);
		idList.forEach(id -> {		
			FwGroup entity = new FwGroup();
			entity.setId(id);
			entity.setDeleted(false);
			baseMapper.updateById(entity);
		});
	}

	@Override
	public void create(FwGroup entity) {
		//校验自行实现
		saveOrUpdate(entity);
	}

	@Override
	public FwGroupVO get(String id) {
		FwGroup entity = baseMapper.selectById(id);
		if (ObjectUtils.isEmpty(entity)) {
			entity = new FwGroup();
		}
		FwGroupVO vo = new FwGroupVO();
		BeanUtils.copyProperties(entity,vo);
		LambdaQueryWrapper<FwFormConfig> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.select(FwFormConfig::getId,FwFormConfig::getName);
		vo.setConfs(fwFormConfigMapper.selectList(queryWrapper));
		return vo;
	}

}